<?php
/**
 * Milestone
 * 
 * @package BugZapp
 * @author Mark Johnson
 * @copyright 2010
 * @version 1.0
 * @access public
 */
class Milestone extends DBObject
{
    var $mid = 0;
    
    var $name = 'Please enter a name.';
    var $date = 0;
    
    /**
     * Milestone::Milestone()
     * 
     * @return
     */
    public function Milestone($data = NULL) 
    {
    	$this->table = 'milestone';
        
        $this->id_name = 'mid';
        
        $this->fields = array(
            'name' => "'%s'",
            'date' => '%d'
        );
        
        parent::DBObject($data);
    }
	
	public static function formatted_date($date)
	{
		return date('M j, Y g:ia', $date);
	}
    
    /**
     * Milestone::schema()
     * 
     * @return
     */
    public static function schema()
    {
        return 'CREATE TABLE IF NOT EXISTS `milestone` (
    			  `mid` int(10) unsigned NOT NULL AUTO_INCREMENT,
    			  `name` varchar(75) NOT NULL,
    			  `date` int(10) unsigned NOT NULL,
    			  PRIMARY KEY (`mid`),
    			  UNIQUE KEY `mid_UNIQUE` (`mid`)
    			) ENGINE=InnoDB DEFAULT CHARSET=utf8';
    }
    
    /**
     * Milestone::install()
     * 
     * @return
     */
    public static function install()
    {
        db_query('DROP TABLE IF EXISTS milestone');
        $result = db_query(Milestone::schema());
        
    	if ($result !== FALSE)
    	{
    		db_query("INSERT INTO milestone (name, date) VALUES ('Please enter a name.', 0)");
    		db_query("UPDATE milestone SET mid=0 WHERE name='Please enter a name.'");
            db_query('ALTER TABLE milestone AUTO_INCREMENT=1');
            
    		system_set_message('The <code>milestone</code> table was successfully created.');
    		
    		return TRUE;
    	}
    	else
    	{
    		system_set_message('The <code>milestone</code> table was not created.');
    		
    		return FALSE;
    	}
    }
    
    /**
     * Milestone::uninstall()
     * 
     * @return
     */
    public static function uninstall()
    {
        $result = db_query('DROP TABLE IF EXISTS milestone');
    	if ($result !== FALSE)
    	{			
    		system_set_message('The <code>milestone</code> table was successfully removed.');
    		
    		return TRUE;
    	}
    	else
    	{
    		system_set_message('The <code>milestone</code> table was not removed.');
    		
    		return FALSE;
    	}
    }
}
